package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 物品收藏功能实现类
 */
@Service
public class GoodLoveServiceImpl extends ServiceImpl<GoodLoveMapper, GoodLove> implements GoodLoveService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的GoodLove表mapper对象
     */
    @Autowired
    private GoodLoveMapper _GoodLoveMpper;

 			
			
 			
    @Autowired
    private GoodMapper _GoodMapper;                        
			

    /**
     * 物品收藏分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<GoodLoveDto> List(GoodLovePagedInput input) {

      
        //声明一个支持物品收藏查询的(拉姆达)表达式
        LambdaQueryWrapper<GoodLove> queryWrapper = Wrappers.<GoodLove>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,GoodLove::getId,input.getId())
                .eq(input.getCreatorId()!=null,GoodLove::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入GoodId不为空,则进行精确查询
        if(input.getGoodId()!=null) {
            queryWrapper=queryWrapper.eq(GoodLove::getGoodId,input.getGoodId());
       	 }
     	 //如果前端搜索传入UserId不为空,则进行精确查询
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(GoodLove::getUserId,input.getUserId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(GoodLove::getCreationTime);
        //构建一个分页查询的model
        Page<GoodLove> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取物品收藏数据
        IPage<GoodLove> pageRecords= _GoodLoveMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _GoodLoveMpper.selectCount(queryWrapper);

        //把GoodLove实体转换成GoodLove传输模型
        List<GoodLoveDto> items= Extension.copyBeanList(pageRecords.getRecords(),GoodLoveDto.class);
        for (GoodLoveDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto UserDTO = new AppUserDto();        
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
           if(UserEntity!=null) {  
          BeanUtils.copyProperties(UserDTO,UserEntity);
            item.setUserDto(UserDTO); 
           }
          
           
          	            
           //查询出关联的Good表信息
           GoodDto GoodDTO = new GoodDto();        
            Good  GoodEntity= _GoodMapper.selectOne(Wrappers.<Good>lambdaQuery().eq(Good::getId,item.getGoodId()));
           if(GoodEntity!=null) {  
          BeanUtils.copyProperties(GoodDTO,GoodEntity);
            item.setGoodDto(GoodDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个物品收藏查询
     */
    @SneakyThrows
    @Override
    public GoodLoveDto Get(GoodLovePagedInput input) {
       if(input.getId()==null)
        {
            return new GoodLoveDto();
        }
        PagedResult<GoodLoveDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new GoodLoveDto();
    }

    /**
     *物品收藏创建或者修改
     */
    @SneakyThrows
    @Override
    public GoodLoveDto CreateOrEdit(GoodLoveDto input) {

        //声明一个物品收藏实体
        GoodLove GoodLove = new GoodLove();

        //把前端传入的input参数拷贝到物品收藏实体
        BeanUtils.copyProperties(GoodLove,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(GoodLove);

        //定义一个返回给前端的物品收藏传输模型
        GoodLoveDto GoodLoveDto=new GoodLoveDto();

        //同理把操作的物品收藏实体拷贝给物品收藏传输模型
        BeanUtils.copyProperties(GoodLoveDto,GoodLove);

        //把传输模型返回给前端
        return GoodLoveDto;
    }



}
